package net.ipinglun.util;


import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
import android.text.TextUtils;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;


public class HttpHelper {

    private static Context mContext;

    private static RequestQueue mRequestQueue;

    private static HttpHelper mInstance;

    private static final String TAG = "Volley";

    public static int TIME_OUT = 10000;

    public static void init(Context context) {
        mContext = context.getApplicationContext();

    }


    public static synchronized HttpHelper getInstance() {
        if (mInstance == null) {
            mInstance = new HttpHelper();
        }

        return mInstance;
    }


    // 异步获取单实例
    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(mContext);
        }

        return mRequestQueue;
    }


    public <T> void addToRequestQueue(Request<T> req, String tag) {
        // set the default tag if tag is empty
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);

        //VolleyLog.d("Adding request to queue: %s", req.getUrl());

        getRequestQueue().add(req);
    }

    /**
     * Adds the specified request to the global queue using the Default TAG.
     *
     * @param req
     * @param tag
     */
    public <T> void addToRequestQueue(Request<T> req) {
        // set the default tag if tag is empty
        req.setTag(TAG);

        getRequestQueue().add(req);
    }


    public void cancelAll(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }

    /*
    static {
        mClient.setTimeout(10000);
    }

    */
}
